#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年3月8日

@author: Administrator
'''
from common.decorators import login_required
from django.db.models.query_utils import Q
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from funcresource import settings
from funcresource.decorators import user_funcresource
from funcresource.models import FuncResource
import copy
import ujson as json


@login_required
@user_funcresource
def index(request, template="funcresource/admin/funcresource/funcresource.html"):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    return TemplateResponse(request, template, rsp_data)
    
@login_required
def list(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = []
    start = int(request.POST['start'])
    length = int(request.POST['length'])
    keyword = request.POST["search[value]"]
    state = request.POST["columns[4][search][value]"]
    state = '' if state == '' else state[1:-1]
    draw = request.POST['draw']
    q = Q()
    if keyword:
        q.add(Q(**{"name__icontains": keyword}), Q.OR)
    if state:
        q.add(Q(**{"state": state}), Q.AND)
    
    count = FuncResource.objects.filter(q).count()
    funcresources = FuncResource.objects.filter(q)[start:start + length]
    for funcresource in funcresources:
        record = {
            "id":funcresource.id,
            "name":funcresource.name,
            "url":funcresource.url,
            "state":funcresource.state,
            "state_desc":settings.get_state_desc(funcresource.state),
            "remark":funcresource.remark,
        }
        rsp_data["data"].append(record)
    rsp_data["recordsTotal"] = count
    rsp_data["recordsFiltered"] = count
    rsp_data["draw"] = draw
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def all(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = {}
    rsp_data["data"]["funcresources"] = []
    funcresources = FuncResource.objects.filter(state=settings.StateEnum.VALID)
    for funcresource in funcresources:
        record = {
            "id":funcresource.id,
            "name":funcresource.name,
        }
        rsp_data["data"]["funcresources"].append(record)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def save(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    id = request.POST.get("funcresource_id", "")
    name = request.POST["name"]
    url = request.POST["url"]
    remark = request.POST["remark"]
    if id:
        if FuncResource.objects.filter(url=url).exclude(id=id).exists():
            rsp_data = copy.copy(settings.ERROR["EXIST_ERR"])
            rsp_data["msg"] = u'资源路径已经存在'
        else:
            FuncResource.objects.filter(id=id).update(name=name, url=url, remark=remark)
    else:
        f_resource = FuncResource.objects.filter(url=url)
        if f_resource:
            f_resource.update(name=name, remark=remark)
        else:
            FuncResource.objects.create(name=name, url=url, remark=remark)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def delete(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    funcresource_ids = request.POST["funcresource_ids"]
    if funcresource_ids:
        funcresource_ids = funcresource_ids.split(",")
        FuncResource.objects.filter(id__in=funcresource_ids, state=settings.StateEnum.VALID).update(state=settings.StateEnum.DELETED)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def reopen(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    funcresource_ids = request.POST["funcresource_ids"]
    if funcresource_ids:
        funcresource_ids = funcresource_ids.split(",")
        FuncResource.objects.filter(id__in=funcresource_ids, state=settings.StateEnum.DELETED).update(state=settings.StateEnum.VALID)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')
    

